﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PACS.Data;
using System.Data;
using WoSoft.PACS.Data.Models;

namespace WoSoft.PACS.Data.CheckItem
{
    public class CheckItemDataService : Db
    {
        /// <summary>
        /// 查询所有行记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetCheckItemFlAllData()
        {
            var dt = SqlDatabase.Sql("SELECT * FROM 检查项目分类 order by 排序").QuerySingle<DataTable>();
            return dt;
        }

        /// <summary>
        /// 查询所有行记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetWordFl()
        {
            var dt = SqlDatabase.Sql("SELECT * FROM 报告词句分类 where isnull(上级ID,'')='' order by 序号").QuerySingle<DataTable>();
            return dt;
        }

        /// <summary>
        /// 插入分类
        /// </summary>
        /// <param name="cname">分类名称</param>
        /// <param name="parentID">上级ID</param>
        /// <param name="comment">注释</param>
        /// <returns></returns>
        public int CheckItemFlInsert(string cname, string parentID, string wordId, string comment)
        {
            return SqlDatabase.Insert("检查项目分类")
                .Column("ID", Guid.NewGuid().ToString())
                .Column("上级ID", parentID)
                .Column("名称", cname)
                .Column("对应词语ID", wordId)
                .Column("注释", comment)
                .Execute();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="p"></param>
        /// <param name="cname"></param>
        /// <param name="parentID"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public int CheckItemFlUpdate(string id, string cname, string parentID, string wordId, string comment)
        {
            return SqlDatabase.Update("检查项目分类")
                .Column("上级ID", parentID)
                .Column("名称", cname)
                .Column("对应词语ID", wordId)
                .Column("注释", comment)
                .Where("ID", id)
                .Execute();
        }

        /// <summary>
        /// 根据ID获取分类结果
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public DataTable GetCheckItemFlByID(string categoryID)
        {
            return SqlDatabase.Sql("select * from 检查项目分类 where ID = @ID")
                .Parameter("ID", categoryID)
                .QuerySingle<DataTable>();
        }

        /// <summary>
        /// 根据分类删除词句分类
        /// </summary>
        /// <param name="categoryID">分类ID</param>
        /// <returns></returns>
        public int CheckItemFlDelete(string categoryID)
        {
            return SqlDatabase.MultiResultSql.Sql("DELETE 检查项目 where 分类ID=@分类ID")
                .Parameter("分类ID", categoryID)
                .Sql(" DELETE 检查项目分类 where ID=@ID")
                .Parameter("ID", categoryID)
                .Execute();
        }

        /// <summary>
        /// 插入词句
        /// </summary>
        /// <param name="flid">分类ID</param>
        /// <param name="xmmc">项目名称</param>
        /// <param name="xmjm">项目简码</param>
        /// <param name="syxb">适用性别</param>
        /// <param name="xmfy">项目费用</param>
        /// <param name="xmms">项目描述</param>
        /// <param name="jdr">建档人</param>
        /// <param name="jdsj">建档时间</param>
        /// <returns></returns>
        public int CheckItemInsert(string flid, string xmmc,string xmjm, string syxb, string xmfy,string xmms,string jdr,string jdsj)
        {
            return this.SqlDatabase.Insert("检查项目")
                .Column("ID", Guid.NewGuid().ToString())
                .Column("分类ID", flid)
                .Column("项目名称", xmmc)
                .Column("项目简码", xmjm)
                .Column("适用性别", syxb)
                .Column("项目费用", xmfy)
                .Column("项目描述", xmms)
                .Column("建档人", jdr)
                .Column("建档时间", jdsj)
                .Execute();
        }

        /// <summary>
        /// 执行更新
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="flid">分类ID</param>
        /// <param name="xmmc">项目名称</param>
        /// <param name="xmjm">项目简码</param>
        /// <param name="syxb">适用性别</param>
        /// <param name="xmfy">项目费用</param>
        /// <param name="xmms">项目描述</param>
        /// <param name="jdr">建档人</param>
        /// <param name="jdsj">建档时间</param>
        /// <returns></returns>
        public int CheckItemUpdate(string id, string flid,string xmmc, string xmjm, string syxb, string xmfy, string xmms, string jdr, string jdsj)
        {
            return this.SqlDatabase.Update("检查项目")
                .Column("分类ID", flid)
                .Column("项目名称", xmmc)
                .Column("项目简码", xmjm)
                .Column("适用性别", syxb)
                .Column("项目费用", xmfy)
                .Column("项目描述", xmms)
                .Column("建档人", jdr)
                .Column("建档时间", jdsj)
                .Where("ID", id)
                .Execute();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int CheckItemDelete(string id)
        {
            return this.SqlDatabase.Sql("DELETE  dbo.检查项目 where ID=@ID")
                .Parameter("ID", id)
                .Execute();
        }

        /// <summary>
        /// 根据ID查询单记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetCheckItemByID(string id)
        {
            return this.SqlDatabase.Sql("select * from 检查项目 where ID = @ID")
                .Parameter("ID", id)
                .QuerySingle<DataTable>();
        }

        /// <summary>
        /// 根据分类获取该分类所有分类ID
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public DataTable GetCheckByCID(string cid)
        {
            return this.SqlDatabase.Sql("select * from 检查项目 where 分类ID = @分类ID")
                           .Parameter("分类ID", cid)
                           .QuerySingle<DataTable>();
        }

    }
}
